package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author 赵星
 * @version 1.0
 * @Date 2023/9/8 21:52
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void savaWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //添加一条菜品数据
        dishMapper.insert(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null || flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                if (dishFlavor.getDishId() == null) {
                    dishFlavor.setDishId(id);
                }
            });
            //添加n条菜品口味
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //要显示的页数 第几页
        int page = dishPageQueryDTO.getPage();
        //每页显示的数量
        int pageSize = dishPageQueryDTO.getPageSize();
        //开启分页查询
        PageHelper.startPage(page, pageSize);

        Page<DishVO> dishes = dishMapper.pageQuery(dishPageQueryDTO);
        long total = dishes.getTotal();
        List<DishVO> records = dishes.getResult();
//        List<DishVO> records = new ArrayList<>();
//        for (Dish dish : disheList) {
//            DishVO dishVO = new DishVO();
//            BeanUtils.copyProperties(dish, dishVO);
//            dishVO.setCategoryName(dishMapper.selectTypeById(dish.getCategoryId()));
//            records.add(dishVO);
//        }
        return new PageResult(total, records);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {

        if (ids.size() > 0 || ids != null) {
            for (Long id : ids) {
                if (dishMapper.selectStatusById(id) == StatusConstant.ENABLE) {//根据菜品id查状态 若为起售状态（1）不能删除
                    throw new DeletionNotAllowedException("商品为起售状态，不能删除！");
                }

                //根据菜品id 到setmeal_dish表 查询该商品是否已经归属于哪一套餐
                if (setmealMapper.selectSetMealDishIdById(id) != 0) {
                    throw new DeletionNotAllowedException("该商品有关联套餐，不能删除！");
                }
                //删除菜品
                dishMapper.delete(id);
                //删除菜品口味
                dishFlavorMapper.deleteFlavorByDishId(id);
            }
        }
    }

    @Override
    public DishVO getDishById(Long id) {
        DishVO dishVO = dishMapper.getDishById(id);
        dishVO.setFlavors(dishFlavorMapper.selectFlavorByDishId(id));
        return dishVO;
    }

    @Transactional
    @Override
    public void updateDish(@RequestBody DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);//更新菜品
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //更改口味 先删除后添加
        dishFlavorMapper.deleteFlavorByDishId(dish.getId());
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            //添加n条菜品口味
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public List<Dish> getDishListByCategoryId(Long categoryId) {
        return dishMapper.getDishListByCategoryId(categoryId);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateDish(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.getDishListByCategoryId(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectFlavorByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
